# -*- coding: utf-8 -*-
'''
Created on 23-11-2012

@author: LONG HOANG GIANG
'''
import sys
import os
import datetime
sys.path.append(os.path.join(os.path.dirname(__file__), '../'))
from CrawlerLib import commonlib
import re

class TangThuVien():
    
    def __init__(self):
        pass
    
    def getDetail(self, url, nickname):
        tree = commonlib.loadweb(url).build_tree()
        for postItem in tree.xpath("//div[@class='page']//table[contains(@id, 'post')]"):
            authorOfPostNode = postItem.xpath("//a[@class='bigusername']")
            if not nickname in commonlib.stringify(authorOfPostNode): continue
            postContent = postItem.xpath("./tr[3]")
            if len(postContent) == 0: continue
            commonlib.Etree.clean_elements(postContent, "img", {})
            commonlib.Etree.clean_elements(postContent, "hr", {})
            commonlib.Etree.clean_elements(postContent, "a", {})
            TTNode = postContent[0].xpath(".//input[@type='button' and contains(@value, 'TT')]")
            if len(TTNode)>0:
                TTNode = TTNode[0]
                TTContentNode = TTNode.xpath("../../div[2]/div[1]")
                if len(TTContentNode)>0:
                    commonlib.Etree.clean(TTContentNode[0])
                    commonlib.Etree.clean(TTNode)
            
#            print commonlib.Etree.tostring(postContent)
            
            textContent = commonlib.html2text(postContent)
            textContent = re.sub(ur"Thảo luận tại đây:.*",'', textContent)
            textContent = re.sub(ur"<[^>]+> *thay đổi nội dung bởi.*",'', textContent)
            textContent = re.sub(ur"( *\n *){3,}","\n\n", textContent)
            print textContent
#            break
            
            
        
    
    def process(self, sid, nickname, start=1):
        url_format = 'http://www.tangthuvien.com/forum/showthread.php?t={0}&page={1}'
        url = url_format.format(sid, start)
        
        self.getDetail(url, nickname)
        

if __name__ == '__main__':
    
    t = TangThuVien()
    t.process('62345', 'thanhhvG', 1)
    print '> Finished at {0}'.format(datetime.datetime.now())
    os._exit(1)